<?php
namespace app\index\controller;

use app\index\model\EnginBaoModel;
use app\index\model\EnginModel;
use app\index\model\EnginMuModel;
use think\Controller;
use think\Exception;
use think\facade\Config;

class Test extends Controller
{

    public function test_add_engin_mu(){

        die;
        // 263 - 279
        $enginidArr = [263,264,265,266,267,268,269,270,271,272,273,274,275,276,277,278,279];
        foreach ($enginidArr as $enginid){
            $enginInfo = EnginModel::where(['id'=>$enginid])->find();
            $muid = EnginMuModel::insertGetId([
                'comid' => 1,
                'enginid' => $enginid,
                'pid' => 0,
                'level' => 1,
                'name' => $enginInfo['name'],
                'created_user' => 0,
                'created_at' => date('Y-m-d H:i:s'),
            ]);
            EnginMuModel::where(['id'=>$muid])->update(['parents'=>$muid]);

            $this->paste_mu(75787, $muid);

            dump($enginid.'_______success');
        }

        dump('all success');die;
    }

    public function paste_mu($copyid, $pasteid){
        $copyInfo = EnginMuModel::where(['id'=>$copyid])->find();
        $pasteInfo = EnginMuModel::where(['id'=>$pasteid])->find();

        if($copyInfo['pid'] > 0){
            //复制文件夹
            $pasteArr = [
                'comid' => $pasteInfo['comid'],
                'enginid' => $pasteInfo['enginid'],
                'pid' => $pasteInfo['id'],
                'parents' => $pasteInfo['parents'],
                'level' => $pasteInfo['level']+1,
                'name' => $this->check_engin_mu_name($copyInfo['name'], $pasteInfo['id'], $pasteInfo['enginid']),
                'created_user' => 0,
                'updated_user' => 0,
                'created_at' => date('Y-m-d H:i:s'),
                'updated_at' => date('Y-m-d H:i:s'),
            ];
            $pasteNewId = EnginMuModel::insertGetId($pasteArr);
            $pasteNewParents = $pasteArr['parents'].','.$pasteNewId;
            EnginMuModel::where(['id'=>$pasteNewId])->update(['parents'=>$pasteNewParents]);
        }else{
            $pasteNewId = $pasteid;
        }


        //复制文件
        $fileArr = EnginBaoModel::where(['comid'=>1,'deleted_user'=>0,'status'=>1,'muid'=>$copyid])->select()->toArray();
        foreach ($fileArr as $fileItem){
            $baoarr = [];
            $baoarr['comid'] = $fileItem['comid'];
            $baoarr['status'] = $fileItem['status'];
            $baoarr['enginid'] = $pasteInfo['enginid'];
            $baoarr['dealid'] = $fileItem['dealid'];
            $baoarr['isadd'] = 1;
            $baoarr['muid'] = $pasteNewId;
            $baoarr['name'] = $fileItem['name'];
            $baoarr['ext'] = $fileItem['ext'];
            $baoarr['size'] = $fileItem['size'];
            $baoarr['oripath'] = $fileItem['oripath'];
            $baoarr['created_user'] = 1;
            $baoarr['updated_user'] = 0;
            $baoarr['created_at'] = date('Y-m-d H:i:s');
            $baoarr['updated_at'] = date('Y-m-d H:i:s');
            $bao_id = EnginBaoModel::insertGetId($baoarr);
            $encpath =  encrypt($bao_id, 'E', Config::get('service.info_key'));
            if(EnginBaoModel::where(['id'=>$bao_id])->update(['encpath'=>$encpath]) === false){
                throw new Exception("上传失败");
            }

        }


        //处理子级
        $childrenArr = EnginMuModel::where(['pid'=>$copyid,'deleted_user'=>0])->select()->toArray();
        foreach ($childrenArr as $chilitem){
            $this->paste_mu($chilitem['id'], $pasteNewId);
        }
    }

    public function check_engin_mu_name($name, $pid=0, $enginid, $id=0){
        $exist = true;
        $num = 1;
        while ($exist) {
            $info = EnginMuModel::where(['comid'=>1,'enginid'=>$enginid,'pid'=>$pid,'name'=>$name,'deleted_user'=>0])
                ->where('id','neq', $id)->find();
            if($info){
                $name = $name.'['.$num.']';
                $exist = true;
            }else{
                $exist = false;
            }
        }
        return $name;
    }






    public function test11(){
        set_time_limit(0);

        $data = EnginMuModel::field('id,pid,name as title')->where(['comid'=>1,'enginid'=>141,'deleted_user'=>0])
            ->order('pid ASC,sort ASC')->select()->toArray();
        $data = array_values($this->listToTree($data));

        dump($data);
        dump('success');
        die;
    }
    public function listToTree($lists, $childKey = 'children'){
        $map = [];
        $res = [];
        foreach($lists as $id => &$item){
            $pid = &$item['pid'];
            $map[$item['id']] = &$item;
            if(!isset($map[$pid])){
                $res[$id] = &$item;
            }else{
                $pItem = &$map[$pid];
                $pItem[$childKey][] = &$item;
            }

            dump($res);
            dump('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
        }
        return $res;
    }












    public function test5(){
        set_time_limit(0);
        $t1 = time();

        $data = EnginMuModel::field('id,pid,name as title,created_at')->where(['comid'=>1,'enginid'=>162,'deleted_user'=>0])
            ->select()->toArray();
        $data = array2tree($data, 'id', 'pid', 'children', $sort= 'created_at');



        dump(json_encode($data));die;

        $t2 = time();
//        dump($t2 - $t1);
        dump($data);die;
    }
    public function test6(){
        set_time_limit(0);
        $t1 = time();

        $data = EnginMuModel::field('id,pid,name as title,created_at')->where(['comid'=>1,'enginid'=>162,'deleted_user'=>0])
            ->order('created_at ASC')->select()->toArray();
        $data = listToTree($data);


        dump(json_encode($data));die;

        $t2 = time();
        dump($t2 - $t1);
        dump($data);die;
    }




    public function test3(){
        set_time_limit(0);
        $t1 = time();


        $str = file_get_contents('teststr.txt');
        $data = json_decode($str, true);
        $data = array2tree($data);



        $t2 = time();
        dump($t2 - $t1);
        dump($data);die;
    }
    public function test4(){
        set_time_limit(0);
        $t1 = time();


        $str = file_get_contents('teststr.txt');
        $data = json_decode($str, true);
        $data = listToTree($data);



        $t2 = time();
        dump($t2 - $t1);
        dump($data);die;
    }


    public function test(){
        set_time_limit(0);
        $t1 = time();

        $data = EnginMuModel::field('id,pid,name as title,created_at')
            ->where(['comid'=>1,'enginid'=>162,'deleted_user'=>0])
            ->select()->toArray();


        foreach ($data as &$item){
            if(in_array($item['id'], [0])){
                $item['spread'] = true;
            }
            $item['basicData'] = ['id'=>$item['id'],'pid'=>$item['pid'],'name'=>$item['title'],'part'=>'dept','parentsName'=>''];
        }



//        $data = array2tree($data, 'id', 'pid', 'children', $sort= 'created_at');

        $str = json_encode($data);

        $t2 = time();
        dump($t2 - $t1);
        dump($str);die;


        dump('success');
        die;
    }




    public function test2(){
        set_time_limit(0);
        $t1 = time();


        $data = $this->get_engin_mu_sons(162,0, []);

        $t2 = time();
        dump($t2 - $t1);
        dump($data);die;


        dump('success');
        die;
    }
    public function get_engin_mu_sons($enginid, $pid=0, $sppaar=[]){
        $data = EnginMuModel::field('id,pid,name as title')->where(['comid'=>1,'pid'=>$pid,'enginid'=>$enginid,'deleted_user'=>0])
            ->order('created_at ASC')->select()->toArray();
        foreach ($data as &$item){
            if(in_array($item['id'], $sppaar)){
                $item['spread'] = true;
            }

            $item['basicData'] = ['id'=>$item['id'],'pid'=>$item['pid'],'name'=>$item['title'],'part'=>'dept','parentsName'=>''];
            $item['children'] = $this->get_engin_mu_sons($enginid, $item['id'], $sppaar);
        }
        return $data;
    }


}
